//
//  Kratos_AccountLoginPersenter.m
//  kk_framework
//
//  Created by 7yao.top on 2018/11/12.
//  Copyright © 2018年 7yao.top. All rights reserved.
//

#import "Kratos_AccountLoginPersenter.h"
#import "Kratos_PresenterProtocol.h"
#import "Kratos_longzhuloginView.h"
#import "UIView+Toast.h"
#import "Kratos_RegisterVC.h"
#import "Kratos_userInfo.h"
#import "Kratos_QuickLoginVC.h"
#import "Kratos_ForgotVC.h"
#import "NSString+Base64.h"
@interface Kratos_AccountLoginPersenter() <Kratos_longzhuloginViewDelegate>
@property (weak,nonatomic) UIViewController *weakVC;

@end

@implementation Kratos_AccountLoginPersenter
- (void)Kratos_loginWithUserName:(NSString *)userName Kratos_pwd:(NSString *)pwd
{
    [self.Kra_httpClient Kratos_post:@"/user/pub/accountpwdapi" Kratos_parameters:@{@"username":userName,@"pwd":pwd} Kratos_onSuccess:^(Kratos_HttpResponse * _Nullable response) {
        if (response.Kra_status == Kratos_HttpResponseStatusSuccess) {
            
        }
        
    } Kratos_onFail:^(Kratos_HttpResponse * _Nullable response) {
        
    }];
}

- (void)Kratos_usageLogic
{
    
    
    
  
    if ([self.view1 isKindOfClass:[Kratos_longzhuloginView class]]) {
        Kratos_longzhuloginView *longzhuLoginView =  (Kratos_longzhuloginView *)self.view1;
        longzhuLoginView.delegate  = self;
    }
    
    
    
    if ([Kratos_userInfo Kratos_getCurrentUserInfo_Kratos].Kra_account.length != 0 &&  [Kratos_userInfo Kratos_getCurrentUserInfo_Kratos].Kra_pwd.length != 0 )
    {
        __weak typeof(self) weakSelf = self;
        Kratos_longzhuloginView *loginView =  (Kratos_longzhuloginView *)weakSelf.view1;
       
        NSString *username =   [Kratos_userInfo Kratos_getCurrentUserInfo_Kratos].Kra_account;
        NSString *password =  [Kratos_userInfo Kratos_getCurrentUserInfo_Kratos].Kra_pwd;
        
        loginView.wefweInputView.phoneInput.Kra_textField.text = username;
        loginView.wefweInputView.pwdinput.Kra_textField.text = password;
    }
    
   
    
    
    
//    NSString * initCallBackStr = [[NSUserDefaults standardUserDefaults] stringForKey:@"InitCallBack"];
//    if ([initCallBackStr isEqualToString:@"NO"]) {
//        Kratos_longzhuloginView *loginView =  (Kratos_longzhuloginView *)self.view1;
//        loginView.wefweInputView.phoneInput.Kra_textField.text = @"test";
//        loginView.wefweInputView.pwdinput.Kra_textField.text = @"123456";
//        [loginView.wefweInputView layoutIfNeeded];
//    }
    
    if ([Kratos_Tool Kratos_getCallback_Kratos] == false) {
        Kratos_longzhuloginView *loginView =  (Kratos_longzhuloginView *)self.view1;
        loginView.wefweInputView.phoneInput.Kra_textField.text = @"test";
        loginView.wefweInputView.pwdinput.Kra_textField.text = @"123456";
        [loginView.wefweInputView layoutIfNeeded];
    }


    
    
    
}
- (void)Kratos_loginButtonClick:(UIButton *)btn
{
    
    __weak typeof(self) weakSelf = self;
    Kratos_longzhuloginView *loginView =  (Kratos_longzhuloginView *)weakSelf.view1;
    NSString *username = loginView.wefweInputView.phoneInput.Kra_textField.text;
    NSString *password = loginView.wefweInputView.pwdinput.Kra_textField.text;
    
    if (username.length == 0 || username == nil) {
        [loginView  makeToast:@"请输入账号"];
        return;
    }
    if (password.length == 0 || password == nil) {
        [loginView  makeToast:@"请输入密码"];
        return;
    }
    if (loginView.agreeniSSelect == false)
    {
        [loginView makeToast:@"必须同意协议才能注册"];
        return;
    }
    
    
    [self.Kra_httpClient Kratos_post:@"/user/pub/accountpwdapi" Kratos_parameters:@{@"username":username,@"password":password} Kratos_onSuccess:^(Kratos_HttpResponse * _Nullable response) {
        if (response.Kra_status == Kratos_HttpResponseStatusSuccess) {
            if ([response.Kra_content[@"data"] isKindOfClass:[NSDictionary class]]) {
                Kratos_userInfo * userInfo =  [[Kratos_userInfo alloc] initWithDict:response.Kra_content[@"data"]];
                userInfo.Kra_account = username;
                userInfo.Kra_pwd = password;
                [userInfo Kratos_saveinfo_Kratos];
                
                [loginView.wefweInputView.phoneInput Kratos_savePhoneInCache];
                [[NSNotificationCenter defaultCenter] postNotificationName:Kra_kLoginSuccessNotification object:userInfo];
            }else
            {
                [loginView makeToast:response.Kra_content[@"msg"]];
            }
           
        }
    } Kratos_onFail:^(Kratos_HttpResponse * _Nullable response) {
        
        
        
        [loginView makeToast:response.Kra_content];
        
        
    }];
}
 

- (void)setViewController:(UIViewController *)ViewController
{
    _weakVC = ViewController;
    
}

- (void)Kratos_presentWithModel:(id)model Kratos_viewController:(UIViewController *)viewController
{
    
}

- (void)Kratos_dissmissVC
{
    [_weakVC dismissViewControllerAnimated:false completion:nil];
}


- (void)Kratos_quickregisterButtonClick:(UIButton *)btn
{
    Kratos_QuickLoginVC *registerVC =   [[Kratos_QuickLoginVC alloc]init];
    [_weakVC presentViewController:registerVC animated:false completion:nil];
}

- (void)Kratos_forgotPWDClick:(UIButton *)btn {
    Kratos_ForgotVC *forgotVC =   [[Kratos_ForgotVC alloc]init];
    [_weakVC presentViewController:forgotVC animated:false completion:nil];
}


- (void)Kratos_getmsgCodeClick:(NSString *)phoneNumber {
    
}


- (void)Kratos_registerButtonClick:(UIButton *)btn {
    Kratos_RegisterVC *registerVC =   [[Kratos_RegisterVC alloc]init];
    [_weakVC presentViewController:registerVC animated:false completion:nil];
}



- (void)Kratos_onFail:(id)clientInfo Kratos_errCode:(NSInteger)errCode errInfo:(NSString *)errInfo {
    
}

- (void)Kratos_onSuccess:(id)responseObject {
    
}

@end
